package com.syni.mdd.sjb.community.base.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.WxExternalContact;
import com.syni.mdd.sjb.common.component.mybatis.community.mapper.WxExternalContactMapper;
import com.syni.mdd.sjb.community.base.service.WxExternalContactService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcc
 * @since 2020-07-23
 */
@Service
@Log4j2
public class WxExternalContactServiceImpl extends ServiceImpl<WxExternalContactMapper, WxExternalContact> implements WxExternalContactService {

    @Autowired
    WxExternalContactMapper wxExternalContactMapper;

    @Override
    public synchronized void addContactList(String corpId,List<String> externalcontactData) {
        if(CollectionUtils.isNotEmpty(externalcontactData)){
            List<WxExternalContact> wxExternalContacts = new ArrayList<>();
            for(String ec : externalcontactData){
                JSONObject jsonObject = JSON.parseObject(ec);
                WxExternalContact wxExternalContact = new WxExternalContact();
                wxExternalContact.setCorpid(corpId);
                JSONObject externalContact = jsonObject.getJSONObject("external_contact");
                wxExternalContact.setCorpFullName(externalContact.getString("corp_full_name"));
                wxExternalContact.setExternalUserid(externalContact.getString("external_userid"));
                wxExternalContact.setName(externalContact.getString("name"));
                wxExternalContact.setCorpName(externalContact.getString("corp_name"));
                wxExternalContact.setPosition(externalContact.getString("position"));
                wxExternalContact.setGender(externalContact.getInteger("gender"));
                JSONArray followUsers = jsonObject.getJSONArray("follow_user");
                if(followUsers.size() > 0){
                    JSONObject followUser = followUsers.getJSONObject(0);
                    log.info("followUser:{}",followUser);
                    wxExternalContact.setFollowDescription(followUser.getString("description"));
                    wxExternalContact.setFollowRemark(followUser.getString("remark"));
                    wxExternalContact.setCreateTime(DateTimeUtils.converDateToLocalDateTime(DateTimeUtils.converIntegerToDate(followUser.getInteger("createtime"))));
                    if(followUser.containsKey("remark_corp_name")) {
                        wxExternalContact.setRemarkCorpName(followUser.getString("remark_corp_name"));
                    }
                    if(followUser.containsKey("add_way")) {
                        wxExternalContact.setAddWay(followUser.getInteger("add_way"));
                    }
                    JSONArray tags = followUser.getJSONArray("tags");
                    if(tags.size() > 0){
                        List<String> groupNames = new ArrayList<>();
                        List<String> tagNames = new ArrayList<>();
                        for(int ii = 0;ii < tags.size();ii++){
                            JSONObject tag = tags.getJSONObject(ii);
                            if(tag.containsKey("group_name")) {
                                groupNames.add(tag.getString("group_name"));
                            }
                            if(tag.containsKey("tag_name")) {
                                tagNames.add(tag.getString("tag_name"));
                            }
                        }
                        if(groupNames.size() > 0){
                            wxExternalContact.setGroupName(StringUtils.join(groupNames,","));
                        }
                        if(tagNames.size() > 0){
                            wxExternalContact.setTagName(StringUtils.join(tagNames,","));
                        }
                    }

                }
                wxExternalContact.setNewTime(LocalDateTime.now());
                wxExternalContacts.add(wxExternalContact);
            };
            if(wxExternalContacts.size() > 0){
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq(WxExternalContact.CORPID,corpId);
                List<WxExternalContact> existList = wxExternalContactMapper.selectList(queryWrapper);
                if(CollectionUtils.isNotEmpty(existList)){
                    Set<Integer> ids = existList.stream().map(WxExternalContact::getId).collect(Collectors.toSet());
                    this.removeByIds(ids);
                }
                this.saveBatch(wxExternalContacts);
            }
        }
    }

    public void deleteContactList(String cropId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(WxExternalContact.CORPID,cropId);
        List<WxExternalContact> existList = wxExternalContactMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(existList)){
            Set<Integer> ids = existList.stream().map(WxExternalContact::getId).collect(Collectors.toSet());
            this.removeByIds(ids);
        }
    }
}
